/** @file         stack.c
 *  @brief        简要说明
 *  @details      详细说明
 *  @author       lzm
 *  @date         2021-09-06 22:52:12
 *  @version      v1.0
 *  @copyright    Copyright By lizhuming, All Rights Reserved
 *  @blog         https://www.cnblogs.com/lizhuming/
 *
 **********************************************************
 *  @LOG 修改日志:
 **********************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 链式结构 */
typedef int se_type; /* 元素类型 */
typedef struct stack_node
{
    se_type date;
    struct stack_node *next;
}stack_node_t;

typedef struct
{
    int count;
    stack_node_t *top;
}stack_link_t;


/**
 * @name   stack_link_creat
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
stack_link_t *stack_link_creat(int size)
{
    stack_link_t *stack_ptr = NULL;

    stack_ptr = (stack_link_t *)malloc(sizeof(stack_link_t));
    if(stack_ptr == NULL)
        return NULL;
    memset(stack_ptr, 0x00, sizeof(stack_link_t));

    stack_ptr->count = 0;
    stack_ptr->top = NULL;

    return stack_ptr;
}

/**
 * @name   stack_link_destroy
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_destroy(stack_link_t *stack)
{
    stack_node_t *stack_cur = NULL;
    stack_node_t *stack_last = NULL;

    if(stack == NULL)
        return -1;
    
    stack_cur = stack->top;
    while(stack_cur)
    {
        stack_last = stack_cur;
        stack_cur = stack_cur->next;
        free(stack_last);
    }

    free(stack);

    return 0;
}

/**
 * @name   stack_link_clear
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_clear(stack_link_t *stack)
{
    stack_node_t *stack_cur = NULL;
    stack_node_t *stack_last = NULL;

    if(stack == NULL)
        return -1;
    
    stack_cur = stack->top;
    while(stack_cur)
    {
        stack_last = stack_cur;
        stack_cur = stack_cur->next;
        free(stack_last);
    }

    free(stack);

    return 0;
}

/**
 * @name   stack_link_empty
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_empty(stack_link_t *stack)
{
    if(stack == NULL)
        return -1;

    if(stack->count == 0)
        return 1;
    
    return 0;
}

/**
 * @name   stack_link_length
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_length(stack_link_t *stack)
{
    if(stack == NULL)
        return -1;

    return (stack->count);
}

/**
 * @name   stack_link_push
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_push(stack_link_t *stack, se_type elem)
{
    stack_node_t *stack_node_ptr = NULL;

    stack_node_ptr = (stack_node_t *)malloc(sizeof(stack_node_t));
    if(stack_node_ptr == NULL)
        return -1;
    memset(stack_node_ptr, 0x00, sizeof(stack_node_t));

    stack_node_ptr->date = elem;
    stack_node_ptr->next = stack->top;
    stack->top = stack_node_ptr->next;
    stack->count++;

    return 0;
}

/**
 * @name   stack_link_pop
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_pop(stack_link_t *stack, se_type *elem)
{
    stack_node_t *node = NULL;
    if(stack_link_empty(stack) != 0)
    {
        elem = NULL;
        return -1;
    }

    *elem = stack->top->date;
    node = stack->top;
    stack->top = stack->top->next;
    free(node);
    stack->count--;

    return 0;
}

/**
 * @name   stack_link_get_top
 * @brief  
 * @param  
 * @retval 
 * @author lzm
 */
int stack_link_get_top(stack_link_t *stack, se_type *elem)
{
    if(stack_link_empty(stack) != 0)
    {
        elem = NULL;
        return -1;
    }

    *elem = stack->top->date;

    return 0;
}
